package com.llkj.contractmanagement.contractmanagement.contract;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.llkj.contractmanagement.main.BaseActivity;
import com.llkj.contractmanagement.R;
import com.llkj.contractmanagement.contractmanagement.adapter.ContractListAdapter;
import com.llkj.contractmanagement.customview.TitleBar;
import com.llkj.contractmanagement.http.HttpMethod;
import com.llkj.contractmanagement.http.UrlConfig;
import com.llkj.contractmanagement.model.ContractListBean;
import com.llkj.contractmanagement.model.UserInfoBean;
import com.llkj.contractmanagement.utils.GsonUtil;
import com.llkj.contractmanagement.utils.ToastUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;

/**
 * author: dong.yj
 * created on: 2016/8/18 17:09
 * description:合同列表界面
 */
public class ContractListActivity extends BaseActivity implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    @Override
    protected int getLayoutResId() {
        return R.layout.activity_contractlist;
    }

    @Bind(R.id.title_contractlist)
    TitleBar titleBar;
    @Bind(R.id.lv_contractlist)
    ListView lv_contractlist;
    private ContractListAdapter adapter;
    private List<ContractListBean.HtlistBean> contractLists;
    private String fl_id = "";
    private UserInfoBean userInfoBean;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        init();
    }

    private void init() {
        initData();
        setListener();
    }

    private void initData() {
        userInfoBean = UserInfoBean.getInstance();
        Intent intent = getIntent();
        if (intent != null) {
            fl_id = getIntent().getStringExtra("fl_id");
        }
        contractLists = new ArrayList<>();
        adapter = new ContractListAdapter(this, contractLists);
        lv_contractlist.setAdapter(adapter);
        requestXqList();
    }

    private void requestXqList() {
        HashMap<String, String> map = new HashMap<>();
        map.put("fl_id", fl_id);
        map.put("user_id", userInfoBean.getId());
        map.put("token", userInfoBean.getToken());
        HttpMethod.xqList(this, this, map, UrlConfig.HTTP_XQ_LIST);
    }

    private void setListener() {
        titleBar.setTopBarClickListener(this);
        lv_contractlist.setOnItemClickListener(this);
        lv_contractlist.setOnItemLongClickListener(this);
    }

    @Override
    public void rightClick() {
        super.rightClick();
        Intent intent = new Intent(this, SearchResultActivity.class);
        //子分类列表搜索，传上一层级的分类id
        intent.putExtra("type", Integer.valueOf(fl_id));
        startActivity(intent);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        if (userInfoBean.getPower1() == 1 && userInfoBean.getPower2() == 1) {
            Intent intent = new Intent(this, FileDetailsActivity.class);
            Bundle bundle = new Bundle();
            bundle.putSerializable("contractDetails", contractLists.get(position));
            intent.putExtras(bundle);
            startActivity(intent);
        } else {
            ToastUtil.makeShortText(this, "您没有相关权限，请联系管理员");
        }
    }

    @Override
    public void onSuccessHttp(String responseInfo, int resultCode) {
        super.onSuccessHttp(responseInfo, resultCode);
        switch (resultCode) {
            case UrlConfig.HTTP_XQ_LIST:
                ContractListBean contractListBean = GsonUtil.GsonToBean(responseInfo, ContractListBean.class);
                if (contractListBean != null) {
                    List<ContractListBean.HtlistBean> addList = contractListBean.getHtlist();
                    if (addList != null && addList.size() > 0) {
                        contractLists.addAll(addList);
                        adapter.notifyDataSetChanged();
                    }
                }
                break;
        }
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        ToastUtil.makeShortText(this, contractLists.get(position).getName());
        return true;
    }
}
